﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Space_Cleaner.SpaceCleaner
{
    class SC_Mouse
    {
        private LinkedList<SC_IMouseListener> listeners;

        private MouseState oldState;

        protected Texture2D texture;

        public SC_Mouse(Texture2D _texture)
        {
            this.listeners = new LinkedList<SC_IMouseListener>();
            this.texture = _texture;
        }

        public void addListener(SC_IMouseListener _listener)
        {
            this.listeners.AddLast(_listener);
        }

        public void update(TimeSpan elapsedTime)
        {
            MouseState ms = Mouse.GetState();
            if (oldState != null)
            {
                int scrolled = ms.ScrollWheelValue - oldState.ScrollWheelValue;

                if (scrolled != 0)
                    notifyMouseWheelMove(scrolled);

                Vector2 mouseDelta = new Vector2(ms.X - oldState.X, ms.Y - oldState.Y);
                if (mouseDelta.Length() > 0.0f)
                    notifyMouseMove(mouseDelta);

                if (ms.LeftButton == ButtonState.Pressed && oldState.LeftButton == ButtonState.Released)
                    notifyLeftButtonPressed();

                if (ms.LeftButton == ButtonState.Released && oldState.LeftButton == ButtonState.Pressed)
                    notifyLeftButtonReleased();

                if (ms.RightButton == ButtonState.Pressed && oldState.RightButton == ButtonState.Released)
                    notifyRightButtonPressed();

                if (ms.RightButton == ButtonState.Released && oldState.RightButton == ButtonState.Pressed)
                    notifyrightButtonReleased();
            }

            oldState = ms;
        }

        private void notifyMouseWheelMove(int _scrolled)
        {
            foreach (SC_IMouseListener listener in this.listeners)
                listener.onMouseWheelMove(_scrolled);
        }

        private void notifyMouseMove(Vector2 _mouseDelta)
        {
            foreach (SC_IMouseListener listener in this.listeners)
                listener.onMouseMove(_mouseDelta);
        }

        private void notifyLeftButtonPressed()
        {
            foreach (SC_IMouseListener listener in this.listeners)
                listener.onLeftButtonPressed();
        }

        private void notifyLeftButtonReleased()
        {
            foreach (SC_IMouseListener listener in this.listeners)
                listener.onLeftButtonReleased();
        }

        private void notifyRightButtonPressed()
        {
            foreach (SC_IMouseListener listener in this.listeners)
                listener.onRightButtonPressed();
        }

        private void notifyrightButtonReleased()
        {
            foreach (SC_IMouseListener listener in this.listeners)
                listener.onRightButtonReleased();
        }

        public virtual void draw(SpriteBatch _spriteBatch)
        {
            _spriteBatch.Draw(texture, new Vector2(oldState.X, oldState.Y), Color.White);
        }

        public Vector2 Position
        {
            get { return new Vector2(oldState.X, oldState.Y); }
        }
    }
}
